#!/usr/bin/env python

import unittest
import nmea2kdb as n2k
from bitsio import BitsIO
import json

class N2KTest(unittest.TestCase):
    def setUp(self):
        print('setup')
        self.n2kdb = n2k.N2KDB()

    def test_DB_Add(self):
        m = n2k.N2KMessage(0)
        self.n2kdb.addtodb(m)
        
    def test_process_str(self):
        #m = n2k.process_str('abcdef1234')
        m = self.n2kdb.received_str('abcdef1234')
        print(m)
        f = n2k.fields(127508, BitsIO(b'\x11\x22\x33\x44\x55\xab' + 
                                      b'\xcd\xef\x00\x11\x22\x33\x44'))
        print(f)
        teststrs = '''0C 4F 9A 26 12 FF A6 E0 09 00 FF FF FF
0C E7 FE 32 00 41 9F 00 02 02 00 00 00
0C 4F 9A 24 12 FF 45 DC FF 7F FF 7F FD
0C E7 FE 32 00 41 9F 00 0A 0A 00 80 00
0C 4F 9A 26 12 FF 00 D9 08 00 FF FF FF
0C 4F 9A 1A 00 FF FF FF 7F B4 07 FF FF
0C 4F 9A 24 12 FF 4E DC FF 7F FF 7F FD
0C C7 FF 18 00 41 9F 04 32 4E 08 F1 FF
0C 4F 9A 26 12 FF 86 4D 07 00 FF FF FF
0C CF BA 26 00 E0 0E FF FF FF FF FF FF
0C CF BA 26 00 E1 34 17 35 00 34 17 35
0C CF BA 26 00 E2 00 FF FF FF FF FF FF
0C 4F 9A 24 12 FF 57 DC FF 7F FF 7F FD
0C 4F 9A 26 12 FF B2 C9 06 00 FF FF FF
0C 4F D8 03 EA D9 24 B9 18 49 8F 7C D5
0C 4F D8 05 EA 72 FC 6C 15 0C 00 FF FF
0C 4F 9A 24 12 FF 5C DC FF 7F FF 7F FD
0C 4F 9A 26 12 FF DF 45 06 00 FF FF FF'''
        for s in teststrs.split('\n'):
            print(s)
            m = self.n2kdb.received_str(s)
            if(m.f != 'unfinished fast packet'):
                print(m.getdict()) 
                
    def testRepeatingFields(self):
        print('test repeating fields')
        teststrs = '''0C CF DC 09 EA E3 68 10 FF FF FF FF F5'''
        for s in teststrs.split('\n'):
            print(s)
            m = self.n2kdb.received_str(s)
            if(m.f != 'unfinished fast packet'):
                print(json.dumps(m.getdict(),indent=4)) 

    def testFromFile(self):
        print('test from file')
        with open('../../../logs/putty2010-10-22_100059.log') as f:
            for l in f:
                l = l.strip()
                m = self.n2kdb.received_str(l)
                print'.',

if __name__ == '__main__':
    unittest.main()
